package com.inossem.warehousestorage_inossem.adapter.outStorage;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.out_storage.OutStorageOtherSelectSpecialInventoryActivity;
import com.inossem.warehousestorage_inossem.bean.outStorage.OutStorageOtherSelectSpecialInventoryBean;

import java.util.List;

/**
 * 作者：wsy on 2017/12/7 14:47
 * <p>
 * 邮箱：445979770@qq.com
 */

public class OtherOutStorageSelectSpecialInventoryAdapter extends BaseAdapter {

    private Context mContext;
    private List<OutStorageOtherSelectSpecialInventoryBean.DataBean.SpecialInventoryListBean> mSpecialInventoryList;
    private LayoutInflater inflater;
    private OutStorageOtherSelectSpecialInventoryActivity.OutStorageOtherSelectSpecialInventoryListener mInStorageOtherSelectSupplierListener;
    private String mSpecialInventoryCode;

    public OtherOutStorageSelectSpecialInventoryAdapter(Context context, List<OutStorageOtherSelectSpecialInventoryBean.DataBean.SpecialInventoryListBean> specialInventoryList, String specialInventoryCode, OutStorageOtherSelectSpecialInventoryActivity.OutStorageOtherSelectSpecialInventoryListener inStorageOtherSelectSupplierListener) {
        mContext = context;
        mSpecialInventoryList = specialInventoryList;
        mSpecialInventoryCode = specialInventoryCode;
        mInStorageOtherSelectSupplierListener = inStorageOtherSelectSupplierListener;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mSpecialInventoryList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return mSpecialInventoryList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.view_in_storage_other_select_supplier_list_first_item, null);
            EditText supplierCode = (EditText) convertView.findViewById(R.id.supplierCode);
            ImageView search = (ImageView) convertView.findViewById(R.id.search);
            supplierCode.setText(mSpecialInventoryCode);
            supplierCode.setHint(mContext.getResources().getString(R.string.activity_inventory_select_speciallnv_code_please_input_special_inventory_code));
            search.setOnClickListener(mInStorageOtherSelectSupplierListener.getNoDoubleClickListener(supplierCode));
        } else {
            OutStorageOtherSelectSpecialInventoryBean.DataBean.SpecialInventoryListBean specialInventoryItem = mSpecialInventoryList.get(position - 1);
            OtherOutStorageSelectSpecialInventoryAdapter.ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.view_in_storage_other_select_supplier_list, null);
                viewHolder = new OtherOutStorageSelectSpecialInventoryAdapter.ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (OtherOutStorageSelectSpecialInventoryAdapter.ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, specialInventoryItem);
        }
        return convertView;
    }

    private void initView(OtherOutStorageSelectSpecialInventoryAdapter.ViewHolder viewHolder, View view) {
        viewHolder.code = (TextView) view.findViewById(R.id.code);
        viewHolder.description = (TextView) view.findViewById(R.id.description);
    }

    private void initData(Context context, OtherOutStorageSelectSpecialInventoryAdapter.ViewHolder viewHolder, View view, OutStorageOtherSelectSpecialInventoryBean.DataBean.SpecialInventoryListBean specialInventoryItem) {
        viewHolder.code.setText(specialInventoryItem.getSpecialInventoryCode());
        viewHolder.description.setText(specialInventoryItem.getSpecialInventoryDescription());
        view.setOnClickListener(mInStorageOtherSelectSupplierListener.select(specialInventoryItem.getSpecialInventoryCode(), specialInventoryItem.getSpecialInventoryDescription()));
    }

    private class ViewHolder {
        private TextView code, description;
    }
}
